﻿using MultiLang;
using System;
using System.Drawing;
using JetBrains.Annotations;

//using Rsdn.Janus.Core.NotifyIcon;
using Rsdn.SmartApp;

namespace Rsdn.Janus
{
	[ActivePart]
	public class NotifyIconActivePart : ActivePartBase
	{
		private static readonly Icon _trayIconEmpty =
			typeof(NotifyIconActivePart).Assembly.LoadIcon(
				ApplicationInfo.ResourcesNamespace + "JanusSmall.ico");           //MLHIDE
		private static readonly Icon _trayIconNew =
			typeof(NotifyIconActivePart).Assembly.LoadIcon(
				ApplicationInfo.ResourcesNamespace + "JanusSmallNew.ico");        //MLHIDE
		private static readonly Icon _trayIconMeUnread =
			typeof(NotifyIconActivePart).Assembly.LoadIcon(
				ApplicationInfo.ResourcesNamespace + "JanusSmallMe.ico");         //MLHIDE

		private readonly INotifyIconService _notifyIconService;
		private readonly IForumsAggregatesService _forumsAggregatesService;
		private IDisposable _aggregatesChangedSubscription;

		public NotifyIconActivePart([NotNull] IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			_notifyIconService = ServiceProvider.GetService<INotifyIconService>();
			_forumsAggregatesService = ServiceProvider.GetService<IForumsAggregatesService>();
		}

		#region Overrides of ActivePartBase

		protected override void ActivateCore()
		{
			if (_notifyIconService == null)
				return;

			UpdateIconAndTooltip();
			_notifyIconService.ContextMenuName = "Application.TrayMenu";       //MLHIDE
			_notifyIconService.Enabled = true;
			_notifyIconService.Visible = true;

			_aggregatesChangedSubscription =
				_forumsAggregatesService.AggregatesChanged.Subscribe(args => UpdateIconAndTooltip());
		}

		protected override void PassivateCore()
		{
			if (_aggregatesChangedSubscription != null)
			{
				_aggregatesChangedSubscription.Dispose();
				_aggregatesChangedSubscription = null;
			}
		}

		#endregion

		private void UpdateIconAndTooltip()
		{
			if (_forumsAggregatesService != null)
			{
				var totalUnread = _forumsAggregatesService.UnreadMessagesCount;
				var meUnread = _forumsAggregatesService.UnreadRepliesToMeCount;

				if (totalUnread > 0)
				{
                    if (meUnread > 0)
                    {
                        _notifyIconService.Tooltip = "{0} {1}".FormatStr( //MLHIDE
                            meUnread,
                            meUnread.GetDeclension(
                                ml.ml_string(1233, "непрочитанный ответ"),//unread answer
                                ml.ml_string(1320, "непрочитанных ответа"),//unread answers
                                ml.ml_string(1232, "непрочитанных ответов")//unread answers
                                ));
                        _notifyIconService.Icon = _trayIconMeUnread;
                    }
                    else
                    {
                        _notifyIconService.Tooltip = "{0} {1}".FormatStr( //MLHIDE
                            totalUnread,
                            totalUnread.GetDeclension(
                                ml.ml_string(1229, "непрочитанное сообщение"),//unread message
                                ml.ml_string(1322, "непрочитанных сообщения"),//unread messages
                                ml.ml_string(1228, "непрочитанных сообщений")//unread messages
                                ));
                        _notifyIconService.Icon = _trayIconNew;
                    }
				    return;
				}
			}
			_notifyIconService.Tooltip = ApplicationInfo.ApplicationName;
			_notifyIconService.Icon = _trayIconEmpty;
		}
	}
}